﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using shenghua.Common.CustomValueType;
using shenghua.Payment.ThirdParties;
using shenghua.Payment.ThirdParties.Alipay;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Payment.Payments
{
    public class PaymentManager: DomainService
    {
        private readonly IRepository<PaymentLog> _paymentLogRepository;
        private readonly AlipayManager _alipayManager;

        public PaymentManager(
            IRepository<PaymentLog> paymentLogRepository,
            AlipayManager alipayManager
            )
        {
            _paymentLogRepository = paymentLogRepository;
            _alipayManager = alipayManager;
        }

        public string GeneratePaymentPage(PaymentPageParams paras, PaymentMethod paymentMethod)
        {
            PaymentPageResult result;

            switch (paymentMethod)
            {
                case PaymentMethod.Alipay:
                    {
                        result = _alipayManager.GeneratePaymentPage(paras);
                        break;
                    }
                case PaymentMethod.WeChat:
                    {
                        throw new Exception("还未开通微信支付接口");
                        break;
                    }
                default:
                    {
                        throw new Exception("无效的支付方式");
                        break;
                    }
            }

            return result.Body;
        }

        public PaymentReturnResult PaymentReturn(PaymentReturnParams paras, PaymentMethod paymentMethod)
        {
            PaymentReturnResult result;

            switch (paymentMethod)
            {
                case PaymentMethod.Alipay:
                    {
                        result = _alipayManager.PaymentReturn(paras);
                        break;
                    }
                case PaymentMethod.WeChat:
                    {
                        throw new Exception("还未开通微信支付接口");
                        break;
                    }
                default:
                    {
                        throw new Exception("无效的支付方式");
                        break;
                    }
            }

            return result;
        }

        public PaymentNotifyResult PaymentNotify(PaymentNotifyParams paras, PaymentMethod paymentMethod)
        {
            PaymentNotifyResult result;

            switch (paymentMethod)
            {
                case PaymentMethod.Alipay:
                    {
                        result = _alipayManager.PaymentNotify(paras);
                        break;
                    }
                case PaymentMethod.WeChat:
                    {
                        throw new Exception("还未开通微信支付接口");
                        break;
                    }
                default:
                    {
                        throw new Exception("无效的支付方式");
                        break;
                    }
            }

            return result;
        }
    }

    /// <summary>
    /// 支付方式，每增加一种支付方式，就在这里去拓展
    /// </summary>
    public enum PaymentMethod
    {
        Alipay = 0,
        WeChat = 1
    }
}
